//
//  SHNetworkReachabilityManager.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2018/2/2.
//  Copyright © 2018年 SH_iOS. All rights reserved.
//
#import "AFNetworkReachabilityManager.h"
#import "SHNetworkReachabilityManager.h"

@implementation SHNetworkReachabilityManager


+ (SHNetworkReachabilityManager *)shareManager{
    static SHNetworkReachabilityManager *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken,^{
        
        instance = [[SHNetworkReachabilityManager alloc] init];
    });
    return instance;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
    }
    return self;
}

#pragma mark----网络检测
+ (void)startMonitoringNetWorkStatuComplete:(NetworkStatuBlock)complete
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    // 提示：要监控网络连接状态，必须要先调用单例的startMonitoring方法
    [manager startMonitoring];
    //检测的结果
    __block typeof(self) weakSelf = self;
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status==0||status==-1) {//没有网络
            //弹出提示框
            [weakSelf showWarningView];
            //将netState值传入block中
            complete(status);
        }else{
            //将netState值传入block中
            complete(status);
        }
    }];
}

#pragma mark---网络断开时弹出提示框
+ (void)showWarningView
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络不给力,请检查网络设置"  preferredStyle:UIAlertControllerStyleAlert];
    
    [alertC addAction:[UIAlertAction actionWithTitle:@"设置" style:0 handler:^(UIAlertAction *action) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }]];
    
    [alertC addAction:[UIAlertAction actionWithTitle:@"好" style:0 handler:^(UIAlertAction *action) {
        
    }]];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertC animated:YES completion:nil];
}


@end
